package com.orchard.xpz.service.gardenFer.gardenFer;

import com.github.pagehelper.PageHelper;
import com.orchard.xpz.dao.GardenFerDao;
import com.orchard.xpz.entity.GardenFer.GardenFer;
import com.orchard.xpz.entity.pesticidesBuy.PesticidesBuy;
import com.orchard.xpz.service.gardenFer.GardenFerService;
import com.orchard.xpz.utils.PageResultData;
import com.orchard.xpz.utils.UserRuntime;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

/**
 * @TaskCode:
 * @description:
 * @author: minshuai.chen
 * @create: 2022/4/19 16:10
 **/
@Service
public class GardenFerServiceImpl implements GardenFerService {

    @Autowired
    private GardenFerDao gardenFerDao;

    @Override
    public PageResultData<GardenFer> getPageList(GardenFer gardenFer) {
        PageHelper.startPage(gardenFer.getPageNum(),gardenFer.getPageSize());
        List<GardenFer> list = gardenFerDao.getList(gardenFer);
        PageResultData<GardenFer> pageResultData = new PageResultData<>(list, gardenFer.getPageNum(), gardenFer.getPageSize());
        return pageResultData;
    }

    @Override
    public boolean saveOrUpdate(GardenFer gardenFer) {
        gardenFer.setGardenId(gardenFer.getGardenPlot().getGardenId());
        gardenFer.setPlantId(gardenFer.getPlantFood().getPlantId());
        if (ObjectUtils.isEmpty(gardenFer.getFerId())){
            gardenFer.setCreateName(UserRuntime.getUser().getUserName());
            gardenFer.setCreateTime(LocalDate.now());
            int i = gardenFerDao.insertSelective(gardenFer);
            return i > 0? true : false;
        }else {
            gardenFer.setUpdateTime(LocalDate.now());
            gardenFer.setUpdateName(UserRuntime.getUser().getUserName());
            int i = gardenFerDao.updateByPrimaryKeySelective(gardenFer);
            return i > 0? true : false;
        }
    }

    @Override
    public boolean deltePlot(String id) {
        return gardenFerDao.deleteByPrimaryKey(id) > 0? true : false;
    }
}
